<template>
  <div class="page-container">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>系统日志管理</h1>
      <p>查看和管理系统操作日志，追踪用户行为和系统事件</p>
    </div>
    
    <div class="log-filter">
      <div class="filter-group">
        <label>日志类型</label>
        <select v-model="filter.type">
          <option value="">全部类型</option>
          <option value="login">登录日志</option>
          <option value="operation">操作日志</option>
          <option value="error">错误日志</option>
          <option value="system">系统日志</option>
        </select>
      </div>
      
      <div class="filter-group">
        <label>操作人</label>
        <input type="text" v-model="filter.user" placeholder="输入用户名">
      </div>
      
      <div class="filter-group">
        <label>日期范围</label>
        <div class="date-range">
          <input type="date" v-model="filter.startDate">
          <span class="separator">至</span>
          <input type="date" v-model="filter.endDate">
        </div>
      </div>
      
      <div class="filter-group">
        <label>关键词</label>
        <input type="text" v-model="filter.keyword" placeholder="搜索日志内容">
      </div>
      
      <div class="filter-actions">
        <button class="btn-primary" @click="searchLogs">查询</button>
        <button class="btn-secondary" @click="resetFilter">重置</button>
        <button class="btn-export" @click="exportLogs">
          <span class="icon-download"></span> 导出日志
        </button>
      </div>
    </div>
    
    <div class="log-table-container">
      <table class="log-table">
        <thead>
          <tr>
            <th>序号</th>
            <th>日志类型</th>
            <th>操作人</th>
            <th>操作时间</th>
            <th>操作IP</th>
            <th>操作内容</th>
            <th>状态</th>
            <th>详情</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(log, index) in filteredLogs" :key="log.id">
            <td>{{ index + 1 }}</td>
            <td><span :class="['log-type', log.type]">{{ getLogTypeName(log.type) }}</span></td>
            <td>{{ log.user }}</td>
            <td>{{ log.time }}</td>
            <td>{{ log.ip }}</td>
            <td class="log-content">{{ log.content }}</td>
            <td><span :class="['log-status', log.status]">{{ log.status === 'success' ? '成功' : '失败' }}</span></td>
            <td>
              <button class="btn-detail" @click="viewLogDetail(log)">详情</button>
            </td>
          </tr>
          <tr v-if="filteredLogs.length === 0">
            <td colspan="8" class="no-data">没有找到匹配的日志记录</td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div class="pagination" v-if="filteredLogs.length > 0">
      <button class="page-btn" :disabled="currentPage === 1" @click="currentPage--">上一页</button>
      <span class="page-info">第 {{ currentPage }} 页，共 {{ totalPages }} 页</span>
      <button class="page-btn" :disabled="currentPage === totalPages" @click="currentPage++">下一页</button>
    </div>
    
    <!-- 日志详情弹窗 -->
    <div class="modal-mask" v-if="showDetail">
      <div class="modal-dialog">
        <div class="modal-header">
          <h3>日志详情</h3>
          <button class="modal-close" @click="showDetail = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="detail-item">
            <span class="detail-label">日志ID：</span>
            <span class="detail-value">{{ currentLog.id }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">日志类型：</span>
            <span class="detail-value">{{ getLogTypeName(currentLog.type) }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">操作人：</span>
            <span class="detail-value">{{ currentLog.user }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">操作时间：</span>
            <span class="detail-value">{{ currentLog.time }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">操作IP：</span>
            <span class="detail-value">{{ currentLog.ip }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">IP地址：</span>
            <span class="detail-value">{{ currentLog.location || '未知' }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">操作内容：</span>
            <span class="detail-value">{{ currentLog.content }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">状态：</span>
            <span class="detail-value"><span :class="['log-status', currentLog.status]">{{ currentLog.status === 'success' ? '成功' : '失败' }}</span></span>
          </div>
          <div class="detail-item" v-if="currentLog.error">
            <span class="detail-label">错误信息：</span>
            <span class="detail-value error-message">{{ currentLog.error }}</span>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn-primary" @click="showDetail = false">关闭</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LogManagement',
  data() {
    return {
      filter: {
        type: '',
        user: '',
        startDate: '',
        endDate: '',
        keyword: ''
      },
      logs: [
        {
          id: 'log-1001',
          type: 'login',
          user: 'admin',
          time: '2023-10-15 08:30:22',
          ip: '192.168.1.100',
          location: '北京市 联通',
          content: '用户登录系统',
          status: 'success'
        },
        {
          id: 'log-1002',
          type: 'operation',
          user: 'admin',
          time: '2023-10-15 09:15:45',
          ip: '192.168.1.100',
          location: '北京市 联通',
          content: '修改系统设置',
          status: 'success'
        },
        {
          id: 'log-1003',
          type: 'operation',
          user: 'operator1',
          time: '2023-10-15 10:22:18',
          ip: '192.168.1.105',
          location: '上海市 电信',
          content: '创建新工单 #20231015001',
          status: 'success'
        },
        {
          id: 'log-1004',
          type: 'error',
          user: 'operator2',
          time: '2023-10-15 11:05:33',
          ip: '192.168.1.106',
          location: '广州市 电信',
          content: '尝试删除工单',
          status: 'failed',
          error: '权限不足，无法删除工单'
        },
        {
          id: 'log-1005',
          type: 'system',
          user: 'system',
          time: '2023-10-15 12:00:00',
          ip: '127.0.0.1',
          location: '本地服务器',
          content: '系统自动备份完成',
          status: 'success'
        },
        {
          id: 'log-1006',
          type: 'login',
          user: 'testuser',
          time: '2023-10-15 13:45:12',
          ip: '192.168.1.110',
          location: '深圳市 联通',
          content: '用户登录系统',
          status: 'failed',
          error: '密码错误'
        },
        {
          id: 'log-1007',
          type: 'operation',
          user: 'admin',
          time: '2023-10-15 14:30:55',
          ip: '192.168.1.100',
          location: '北京市 联通',
          content: '分配工单 #20231015001 给 operator3',
          status: 'success'
        },
        {
          id: 'log-1008',
          type: 'operation',
          user: 'operator3',
          time: '2023-10-15 15:20:33',
          ip: '192.168.1.108',
          location: '杭州市 电信',
          content: '关闭工单 #20231015001',
          status: 'success'
        }
      ],
      currentPage: 1,
      pageSize: 5,
      showDetail: false,
      currentLog: null
    };
  },
  computed: {
    filteredLogs() {
      let result = [...this.logs];
      
      // 按类型筛选
      if (this.filter.type) {
        result = result.filter(log => log.type === this.filter.type);
      }
      
      // 按用户筛选
      if (this.filter.user) {
        result = result.filter(log => log.user.includes(this.filter.user));
      }
      
      // 按日期范围筛选
      if (this.filter.startDate) {
        result = result.filter(log => new Date(log.time) >= new Date(this.filter.startDate));
      }
      
      if (this.filter.endDate) {
        const endDate = new Date(this.filter.endDate);
        endDate.setHours(23, 59, 59, 999);
        result = result.filter(log => new Date(log.time) <= endDate);
      }
      
      // 按关键词筛选
      if (this.filter.keyword) {
        const keyword = this.filter.keyword.toLowerCase();
        result = result.filter(log => 
          log.content.toLowerCase().includes(keyword) || 
          log.user.toLowerCase().includes(keyword) ||
          log.ip.includes(keyword)
        );
      }
      
      // 分页处理
      const startIndex = (this.currentPage - 1) * this.pageSize;
      return result.slice(startIndex, startIndex + this.pageSize);
    },
    totalPages() {
      let total = this.logs.length;
      
      // 计算符合筛选条件的总记录数
      if (this.filter.type) {
        total = this.logs.filter(log => log.type === this.filter.type).length;
      }
      
      if (this.filter.user) {
        total = this.logs.filter(log => log.user.includes(this.filter.user)).length;
      }
      
      if (this.filter.startDate || this.filter.endDate || this.filter.keyword) {
        // 这里简化处理，实际应重新计算
        total = this.filteredLogs.length;
      }
      
      return Math.ceil(total / this.pageSize);
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    getLogTypeName(type) {
      const typeMap = {
        'login': '登录日志',
        'operation': '操作日志',
        'error': '错误日志',
        'system': '系统日志'
      };
      return typeMap[type] || type;
    },
    searchLogs() {
      this.currentPage = 1; // 重置到第一页
    },
    resetFilter() {
      this.filter = {
        type: '',
        user: '',
        startDate: '',
        endDate: '',
        keyword: ''
      };
      this.currentPage = 1;
    },
    exportLogs() {
      this.$message.success('正在导出日志数据...');
    },
    viewLogDetail(log) {
      this.currentLog = log;
      this.showDetail = true;
    }
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1e5e8;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

h1 {
  margin: 15px 0 10px;
  color: #2c3e50;
}

.page-header p {
  color: #7f8c8d;
  margin: 0;
}

.log-filter {
  background: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 15px;
}

.filter-group {
  flex: 1;
  min-width: 200px;
}

.filter-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #34495e;
}

.filter-group input,
.filter-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #e1e5e8;
  border-radius: 5px;
}

.date-range {
  display: flex;
  align-items: center;
  gap: 10px;
}

.date-range input {
  width: 50%;
}

.separator {
  color: #7f8c8d;
}

.filter-actions {
  display: flex;
  gap: 10px;
  min-width: auto;
}

.btn-primary {
  background: #3498db;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn-primary:hover {
  background: #2980b9;
}

.btn-secondary {
  background: #ecf0f1;
  color: #34495e;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn-secondary:hover {
  background: #dcdde1;
}

.btn-export {
  background: #2ecc71;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-export:hover {
  background: #27ae60;
}

.log-table-container {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.log-table {
  width: 100%;
  border-collapse: collapse;
}

.log-table th,
.log-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #f1f1f1;
}

.log-table th {
  background-color: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.log-type {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.log-type.login {
  background-color: #e8f4fd;
  color: #3498db;
}

.log-type.operation {
  background-color: #eafaf1;
  color: #27ae60;
}

.log-type.error {
  background-color: #fdedeb;
  color: #e74c3c;
}

.log-type.system {
  background-color: #fef5e7;
  color: #f39c12;
}

.log-status {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.log-status.success {
  background-color: #eafaf1;
  color: #27ae60;
}

.log-status.failed {
  background-color: #fdedeb;
  color: #e74c3c;
}

.log-content {
  max-width: 300px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-detail {
  background: #3498db;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
}

.no-data {
  text-align: center;
  padding: 50px 0;
  color: #7f8c8d;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
  margin-top: 20px;
  padding: 10px;
}

.page-btn {
  padding: 5px 12px;
  border: 1px solid #e1e5e8;
  background-color: white;
  border-radius: 4px;
  cursor: pointer;
}

.page-btn:disabled {
  color: #bdc3c7;
  cursor: not-allowed;
}

.page-info {
  color: #34495e;
}

/* 弹窗样式 */
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-dialog {
  background-color: white;
  border-radius: 8px;
  width: 600px;
  max-width: 90%;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #f1f1f1;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
}

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #7f8c8d;
}

.modal-body {
  padding: 20px;
}

.detail-item {
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f1f1f1;
}

.detail-item:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.detail-label {
  display: inline-block;
  width: 100px;
  font-weight: 500;
  color: #34495e;
}

.detail-value {
  color: #2c3e50;
}

.error-message {
  color: #e74c3c;
  word-break: break-all;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #f1f1f1;
  text-align: right;
}
</style>